package JUC.wn_syn_vlo;

/*
    （变量必须stat// 不stat 赋值完都是1 别人改了看不到 有syn不用v了

    // 用匿名好 要不class要static
    对象锁 不能被static干扰
    -> main自己递归new自己 不用static

 */
public class syn对象锁_Thread implements Runnable{
    static int x = 0;
    @Override
    public synchronized void run() { // 2个new对象 失效等于没用
        for (int i = 0; i < 3000000; i++) {
            x++;
        }
    }

    public static void main(String[] args) throws Exception{
        syn对象锁_Thread rangzi1 = new syn对象锁_Thread();


        // 6000000
//        Thread Thr1 = new Thread(rangzi1);
//        Thr1.start();
//        Thread Thr2 = new Thread(rangzi1);
//        Thr2.start();

        // 3482451
//        Thread Thr1 = new Thread(new syn对象锁类锁());
//        Thr1.start();
//        Thread Thr2 = new Thread(new syn对象锁类锁());
//        Thr2.start();

        // 如何不用new一个穰子 -> 用syn解决上方 ? -> 看syn类锁
        Thread Thr1 = new Thread(new syn对象锁_Thread());
        Thr1.start();
        Thread Thr2 = new Thread(new syn对象锁_Thread());
        Thr2.start();

        Thr1.join();
        Thr2.join();
        System.out.println(x);
    }
}
